package com.yicong.ants.view.dialog;


import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.InputFilter;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.SizeUtils;
import com.cchao.simplelib.core.UiHelper;
import com.cchao.simplelib.util.CallBacks;
import com.yicong.ants.R;
import com.yicong.ants.utils.DisplayUtils;
import com.yicong.ants.utils.ToastUtils;

/**
 * 补全姓氏
 */
public class CompleteLastNameDialog extends BaseNiceDialog implements View.OnClickListener {
    private Context mContext;
    private CompleteLastNameInfo info;
    private CallBacks.Str callBack;

    public static CompleteLastNameDialog newInstance(Context context, CompleteLastNameInfo info) {
        CompleteLastNameDialog dialog = new CompleteLastNameDialog(info, context);
        dialog.setWidth(DisplayUtils.px2dp(context, (float) (UiHelper.getScreenWidth() * 0.9)))
                .setOutCancel(false)
                .setDimAmount(0.3f);
        return dialog;
    }

    public CompleteLastNameDialog(CompleteLastNameInfo info, Context context) {
        this.mContext = context;
        this.info = info;
    }

    public void setOnClickListener(CallBacks.Str callBack) {
        this.callBack = callBack;
    }

    @Override
    public int intLayoutId() {
        return R.layout.dialog_surname;
    }


    @Override
    public void convertView(NiceViewHolder holder, BaseNiceDialog dialog) {
        holder.setText(R.id.phone, info.phone);
        LinearLayout layout = holder.getView(R.id.name_layout);
        layout.removeAllViews();
        String name = info.name;
        for (int i = 0; i < name.length(); i++) {
            if (i == 0) {
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(SizeUtils.dp2px(53), ViewGroup.LayoutParams.MATCH_PARENT);
                layout.addView(newTextView(0, "姓"), params);
            } else if (i == name.length() - 1) {
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(SizeUtils.dp2px(53), ViewGroup.LayoutParams.MATCH_PARENT);
                layout.addView(newTextView(2, String.valueOf(name.charAt(i))), params);
            } else {
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(SizeUtils.dp2px(53), ViewGroup.LayoutParams.MATCH_PARENT);
                layout.addView(newTextView(1, String.valueOf(name.charAt(i))), params);
            }
        }

        holder.setOnClickListener(R.id.close, this);
        holder.setOnClickListener(R.id.tv_sure, this);
    }

    private EditText editText;

    //0 开始 1中间 2结束
    private View newTextView(int status, String text) {
        if (status == 0) {
            editText = new EditText(mContext);
            editText.setFilters( new InputFilter[]{new InputFilter.LengthFilter(1)});
            editText.setTypeface(Typeface.DEFAULT_BOLD);
            editText.setBackgroundResource(R.drawable.dialog_surname_edt_start_bg);
            editText.setGravity(Gravity.CENTER);
            editText.setHint(text);
            editText.setLines(1);
            editText.setTextColor(Color.parseColor("#ff323233"));
            editText.setTextSize(17);
            return editText;
        } else if (status == 2) {
            TextView textView = new TextView(mContext);
            textView.setTypeface(Typeface.DEFAULT_BOLD);
            textView.setTypeface(Typeface.DEFAULT_BOLD);
            textView.setBackgroundResource(R.drawable.dialog_surname_edt_end_bg);
            textView.setGravity(Gravity.CENTER);
            textView.setText(text);
            textView.setTextColor(Color.parseColor("#ff323233"));
            textView.setTextSize(17);
            return textView;
        } else {
            TextView textView = new TextView(mContext);
            textView.setTypeface(Typeface.DEFAULT_BOLD);
            textView.setBackgroundResource(R.drawable.dialog_surname_edt_center_bg);
            textView.setGravity(Gravity.CENTER);
            textView.setText(text);
            textView.setTextColor(Color.parseColor("#ff323233"));
            textView.setTextSize(17);
            return textView;
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.close) {
            dismiss();
        } else if (v.getId() == R.id.tv_sure) {
            if (editText.getText() == null || editText.getText().toString().length() == 0){
                ToastUtils.showShort(v.getContext() , "请补全姓氏");
                return;
            }
            dismiss();
            if (callBack != null) {
                callBack.onCallBack(editText.getText().toString());
            }
        }
    }

    public static class CompleteLastNameInfo {
        public String name;
        public String phone;
    }
}
